<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\controller\Base;
use think\Db;
use think\Session;

class Classes extends Base
{
    public function lst()//列表
    {
        $d_lst=db('class')->alias('l')->join('coach c','c.coach_id=l.coach_id')->select();
        $this->assign('info',$d_lst);
        return $this->fetch();
    }
  
    
    public function add()//添加逻辑 添加时默认课程状态为0，预备中
    {
     $club=db('coach')->select();
     $this->assign('club',$club);
     if(request()->isPost()){
         $data=[
             'class_name'=>input('class_name'),
             'class_desc'=>input('class_desc'),
             'class_stime'=>input('class_stime'),
             'class_etime'=>input('class_etime'),
             'coach_id'=>input('coach_id'),
             'class_status'=>0,
         ];
         $res=db('class')->insert($data);
         if($res){
             $this->success('添加课程成功！','lst');
         }else{
             $this->error('添加课程失败...');
         }
        
     }
      return $this->fetch();
 }

 public function edit()//改动页面
 {
     $c_id=input('class_id');
     $c_info=db('class')->where('class_id',$c_id)->select();
     $club=db('coach')->select();
     $this->assign('club',$club);
     $this->assign('info',$c_info);
     return $this->fetch();
 }


 public function doedit()//改动方法
 {
 $class_id=input('class_id');
 if(request()->isPost()){
             $data=[
                'class_name'=>input('class_name'),
                'class_desc'=>input('class_desc'),
                'class_stime'=>input('class_stime'),
                'class_etime'=>input('class_etime'),
                'coach_id'=>input('coach_id'),
                 
             ];
             $res=db('class')->where('class_id',$class_id)->update($data);
             if($res){
                 $this->success('修改课程信息成功！','lst');
             }else{
                 $this->error('修改课程信息失败...');
             }
         }

     }




    public function del()//删除课程
    {  
        $daily_id=input('class_id');
        $del=db('class')->where('class_id',$daily_id)->delete();
        if($del)
        {
            return $this->success('删除课程成功！');
        }
        else
        {
            return $this->error('删除课程失败');
        }
    }

    public function checkstatus()//改变课程状态 
    {
        $daily_id=input('class_id');
        $daily_status=db('class')->where('class_id',$daily_id)->field('class_status')->find();
        if($daily_status['class_status']==0)
        {
            $daily_statusl=['class_status'=>1];
            
            $res=db('class')->where("class_id",$daily_id)->update($daily_statusl);
            if($res)
            {
                return $this->success('您已经冻结课程成功','lst');//跳转到用户列表页
            }
            else{
                return $this->error('您冻结课程失败!');
        }
        }
    }
    
    // public function cllst()
    // {
    //     $user_id=input('user_id');
    //     $class=db('class')->alias('l')->where('l.user_id',$user_id)->join('user u',"u.user_id=$user_id")->select();
    //  //    dump($comment);die();
    //     $this->assign('comment',$class);
    //     return $this->fetch('lst');
    // }

    // public function cldetails()
    // {
    //     $user_id=input('user_id');
    //     $class_id=input('class_id');
    //     $class_info=db('class')->alias('l')->where('class_id',$class_id)->join('user u',"l.user_id=$user_id")->find();
    //     // dump($class_info);die();
    //     $this->assign('class',$class_info);
    //     return $this->fetch('details');
    // }

}